package com.nbjtjc.util;

import com.nbjtjc.safe.model.safe.RiskPoint;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class InsertRiskPoint {

    public static void main(String[] args) {

    }

    public static List<RiskPoint>  parseExcel(){
        List<RiskPoint> riskPoints=new ArrayList<>();

        String filePath = "E:\\aaa\\aaa.xlsx";
        InputStream fis = null;
        try {
            fis = new FileInputStream(filePath);
            Workbook workbook = null;
            if (filePath.endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(fis);
            } else if (filePath.endsWith(".xls") || filePath.endsWith(".et")) {
                workbook = new HSSFWorkbook(fis);
            }
            fis.close();
            /* 读EXCEL文字内容 */
            // 获取第一个sheet表，也可使用sheet表名获取
            Sheet sheet = workbook.getSheetAt(0);
            // 获取行
            Iterator<Row> rows = sheet.rowIterator();
            Row row;
            Cell cell;
            Map<String,Integer> riskTypeMap=new HashMap<>();
            while (rows.hasNext()) {
                RiskPoint riskPoint=new RiskPoint();
                row = rows.next();
                riskPoint.setEnterpriseId(0);
                riskPoint.setPointName(row.getCell(0).getStringCellValue().trim());
                String riskTypeName=row.getCell(1).getStringCellValue().trim();
                riskPoint.setRiskTypeName(riskTypeName);
                if(riskTypeMap.containsKey(riskTypeName)){
                    riskPoint.setRiskType(riskTypeMap.get(riskTypeName));
                }else{
                    riskTypeMap.put(riskTypeName,riskTypeMap.size()+1);
                }

                // 获取单元格
                Iterator<Cell> cells = row.cellIterator();
                while (cells.hasNext()) {
                    cell = cells.next();
                    String cellValue = cell.getStringCellValue().trim();
                    System.out.print(cellValue + " ");
                }
                System.out.println();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return riskPoints;
    }


}
